package com.woniuxy.house.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.house.entity.Comment;
import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.Source;
import com.woniuxy.house.mapper.CommentMapper;
import com.woniuxy.house.service.CommentService;
import com.woniuxy.house.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 评论表serviceimpl
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 增加评论表
     */
    @Override
    public void addComment(Comment comment) {
        comment.setTime(LocalDateTime.now().toString());

        commentMapper.addComment(comment);
    }

    /**
     * 修改评论表
     */
    @Override
    public void updateComment(Comment comment) {
        commentMapper.updateComment(comment);
    }

    /**
     * 删除评论表
     */
    @Override
    public void deleteComment(Integer id) {
        commentMapper.deleteComment(id);
    }

    /**
     * 查询全部的评论表
     */
    @Override
    public List<Comment> getAll() {
        List<Comment> comments=commentMapper.getAll();
        return comments;
    }

    /**
     * 根据用户id查询的评论表
     */
    @Override
    public List<Comment> getByUserId(Integer userId) {
        return commentMapper.getByUserId(userId);
    }

    /**
     * 根据帖子id查询的评论表
     */
    @Override
    public PageBean<Comment> getByPostId(Integer postId,Integer page) {
        PageBean<Comment> pageBean = new PageBean<>();
        pageBean.setPageSzie(5);//分页大小
        pageBean.setCurrPage(page);    //设置当前页数
        Page p = PageHelper.startPage(pageBean.getCurrPage(), pageBean.getPageSzie());
        PageHelper.orderBy("time desc");
      List<Comment>comments=  commentMapper.getByPostId(postId);
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(comments);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }

    @Override
    public PageBean<Comment> getByPage(int page) {
        int pagesize=2;
        PageBean<Comment> pageBean=new PageBean<>();
        pageBean.setPageSzie(pagesize);
        pageBean.setCurrPage(page);
        Page<Comment> p = PageHelper.startPage(page, pagesize);
        PageHelper.orderBy("time desc");
        List<Comment> Comments = commentMapper.getAll();
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(p);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;

    }

    @Override
    public PageBean<Comment> getByCondition(CommentVo commentVo) {
        int pagesize=2;
        PageBean<Comment> pageBean=new PageBean<>();
        pageBean.setPageSzie(pagesize);
        pageBean.setCurrPage(commentVo.getPage());
        Page<Comment> p = PageHelper.startPage(commentVo.getPage(), pagesize);
        PageHelper.orderBy("time desc");
        List<Comment> Comments = commentMapper.getByCondition(commentVo.getAccount(),commentVo.getTitle());
        pageBean.setTotalNums((int) p.getTotal());//设置总数量
        pageBean.setTotalPage(p.getPages());
        pageBean.setData(p);//设置当前页数的数据
        pageBean.setCurrpageSzie(pageBean.getData().size());//当前页数据数量
        return pageBean;
    }
}
